// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//
#pragma once

#include <pollux/type/simple_function_api.h>
#include <pollux/type/type.h>

namespace kumo::pollux {
    class TDigestType : public VarbinaryType {
    public:
        static const std::shared_ptr<const TDigestType> &get(
            const TypePtr &dataType) {
            // Only TDIGEST(DOUBLE) exists in Presto so we use a singleton to improve
            // performance.
            POLLUX_CHECK(dataType->is_double());
            static const auto instance =
                    std::shared_ptr<const TDigestType>(new TDigestType(DOUBLE()));
            return instance;
        }

        bool equivalent(const Type &other) const override {
            // Pointer comparison works since this type is a singleton.
            return this == &other;
        }

        const char *name() const override {
            return "TDIGEST";
        }

        const std::vector<TypeParameter> &parameters() const override {
            return parameters_;
        }

        std::string toString() const override {
            return fmt::format("TDIGEST({})", parameters_[0].type->toString());
        }

        melon::Dynamic serialize() const override {
            melon::Dynamic obj = melon::Dynamic::object;
            obj["name"] = "Type";
            obj["type"] = name();
            melon::Dynamic children = melon::Dynamic::array;
            for (auto &param: parameters_) {
                children.push_back(param.type->serialize());
            }
            obj["cTypes"] = children;
            return obj;
        }

    private:
        explicit TDigestType(const TypePtr &dataType)
            : parameters_({TypeParameter(dataType)}) {
        }

        const std::vector<TypeParameter> parameters_;
    };

    inline std::shared_ptr<const TDigestType> TDIGEST(const TypePtr &dataType) {
        return TDigestType::get(dataType);
    }

    // Type to use for inputs and outputs of simple functions, e.g.
    // arg_type<TDigest> and out_type<TDigest>.
    template<typename T>
    struct SimpleTDigestT;

    template<>
    struct SimpleTDigestT<double> {
        using type = Varbinary;
        static constexpr const char *typeName = "tdigest(double)";
    };

    template<typename T>
    using SimpleTDigest = CustomType<SimpleTDigestT<T> >;
} // namespace kumo::pollux
